package com.pcbly.web.persistence.workflow;

import java.util.List;

import org.hibernate.Query;

import com.pcbly.web.bean.workflow.WorkflowStepBean;
import com.pcbly.web.bean.workflow.WorkflowTemplateBean;
import com.pcbly.web.bean.workflow.WorkflowTransitionBean;
import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.persistence.HibernateBaseDao;

@SuppressWarnings("unchecked")
public class WorkflowTransitionHibernateDao extends
		HibernateBaseDao<WorkflowTransitionBean> implements
		WorkflowTransitionDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3624646971933781902L;

	public WorkflowTransitionHibernateDao() {
		super();
	}

	protected Class<WorkflowTransitionBean> getEntityClass() {
		return WorkflowTransitionBean.class;
	}

	public void createWorkflowTransition(WorkflowTransitionBean bean)
			throws Exception {
		bean.setId((new GUID()).toString());
		getSession().save(bean);
	}

	public WorkflowStepBean findWorkflowStepById(String id) throws Exception {
		return (WorkflowStepBean) getSession().get(WorkflowStepBean.class, id);
	}

	public WorkflowTemplateBean findWorkflowTemplateById(String id)
			throws Exception {
		return (WorkflowTemplateBean) getSession().get(
				WorkflowTemplateBean.class, id);
	}

	
	@Override
	public List<WorkflowTransitionBean> findWorkflowTransitionBeansByStepId(
			String templateId, String preStepId, String nextStepId)
			throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder();
		sb.append("select a from WorkflowTransitionBean a ");
		sb.append(" where a.workflowTemplate.id=? and ");
		sb.append(" a.fromStep.id=? and  ");
		sb.append(" a.toStep.id=?  ");
		Query query= getSession().createQuery(sb.toString());
		query.setString(0, templateId);
		query.setString(1, preStepId);
		query.setString(2, nextStepId);
		
		return query.list();
	}
}
